home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / manage / snmp / mit / bsd / udp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-17  |  1.7 KB  |  107 lines

  1.  
  2. /*
  3.  *    $Header: udp.c,v 3.0 91/05/17 16:14:08 jrd Rel $
  4.  *    Author: J. Davin
  5.  *    Copyright 1988, 1989, Massachusetts Institute of Technology
  6.  *    See permission and disclaimer notice in file "notice.h"
  7.  */
  8.  
  9. #include    <notice.h>
  10.  
  11. #include    <sys/types.h>
  12. #include    <sys/socket.h>
  13. #include    <netinet/in.h>
  14.  
  15. #include    <ctypes.h>
  16. #include    <debug.h>
  17. #include    <local.h>
  18. #include    <udp.h>
  19.  
  20. typedef        struct            UdpTag {
  21.  
  22.         int            udpSocket;
  23.         struct    sockaddr    udpSockAddr;
  24.         CIntfType        udpRefCnt;
  25.  
  26.         }            UdpType;
  27.  
  28. typedef        UdpType        *UdpPtrType;
  29.  
  30. SmpStatusType    udpSend (udp, bp, n)
  31.  
  32. SmpSocketType        udp;
  33. CBytePtrType        bp;
  34. CIntfType        n;
  35.  
  36. {
  37.     UdpPtrType        tp;
  38.     int            result;
  39.  
  40.     if (udp == (SmpSocketType) 0) {
  41.         return (errBad);
  42.     }
  43.  
  44.     DEBUG0 ("udpSend:\n");
  45.     DEBUGBYTES (bp, n);
  46.     DEBUG0 ("\n");
  47.  
  48.     tp = (UdpPtrType) udp;
  49.     do {    
  50.         result = sendto (tp->udpSocket, (char *) bp,
  51.             (int) n, (int) 0,
  52.             & (tp->udpSockAddr), sizeof (struct sockaddr_in));
  53.         n -= result;
  54.         bp += result;
  55.  
  56.     } while ((result > 0) && (n > 0));
  57.  
  58.     if (result < 0) {
  59.         perror ("udpSend");
  60.         return (errBad);
  61.     }
  62.     else {
  63.         return (errOk);
  64.     }
  65. }
  66.  
  67. SmpSocketType    udpNew (so, host, port)
  68.  
  69. int            so;
  70. u_long            host;
  71. u_short            port;
  72.  
  73. {
  74.     UdpPtrType        tp;
  75.     struct    sockaddr_in    *sin;
  76.  
  77.     tp = (UdpPtrType) malloc ((unsigned) sizeof (*tp));
  78.     if (tp != (UdpPtrType) 0) {
  79.         (void) bzero ((char *) tp, (int) sizeof (*tp));
  80.         tp->udpSocket = so;
  81.         tp->udpRefCnt = 1;
  82.         sin = (struct sockaddr_in *) & tp->udpSockAddr;
  83.         sin->sin_family = AF_INET;
  84.         sin->sin_port = port;
  85.         sin->sin_addr.s_addr = host;
  86.     }
  87.  
  88.     return ((SmpSocketType) tp);
  89. }
  90.  
  91. SmpSocketType    udpFree (udp)
  92.  
  93. SmpSocketType    udp;
  94.  
  95. {
  96.     UdpPtrType        tp;
  97.  
  98.     if (udp != (SmpSocketType) 0) {
  99.         tp = (UdpPtrType) udp;
  100.         if (--tp->udpRefCnt <= 0) {
  101.             (void) free ((char *) tp);
  102.         }
  103.     }
  104.     return ((SmpSocketType) 0);
  105. }
  106.  
  107.